//
//  CMPedometerVC.swift
//  UIScrollView的快速使用
//
//  Created by hongfei tam on 2019/6/20.
//  Copyright © 2019 YQ05. All rights reserved.
//


import UIKit
import CoreMotion
    
class CMPedometerVC: UIViewController {
        //用于显示实时信息
//        @IBOutlet weak var textView: UITextView!
        
        //计步器对象
        let pedometer = CMPedometer()
        var textView: UITextView! = nil
    
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.view.backgroundColor = UIColor.green
            textView = UITextView.init(frame: CGRect(x: 0, y: 200, width: UISCREEN_WIDTH, height: 600))
            self.view.addSubview(textView)
            
            //开始计步器更新
            startPedometerUpdates()
        }
        
        // 开始获取步数计数据
        func startPedometerUpdates() {
            //判断设备支持情况
            guard CMPedometer.isStepCountingAvailable() else {
                self.textView.text = "\n当前设备不支持获取步数\n"
                return
            }
            
            //获取今天凌晨时间
            let cal = Calendar.current
            var comps = cal.dateComponents([.year, .month, .day], from: Date())
            comps.hour = 0
            comps.minute = 0
            comps.second = 0
            let midnightOfToday = cal.date(from: comps)!
            
            //初始化并开始实时获取数据
            self.pedometer.startUpdates (from: midnightOfToday, withHandler: { pedometerData, error in
                //错误处理
                guard error == nil else {
                    print(error!)
                    return
                }
                
                //获取各个数据
                var text = "---今日运动数据---\n"
                 text += "時間: \(comps)\n"
                if let numberOfSteps = pedometerData?.numberOfSteps {
                    text += "步数: \(numberOfSteps)\n"
                }
                if let distance = pedometerData?.distance {
                    text += "距离: \(distance)\n"
                }
                if let floorsAscended = pedometerData?.floorsAscended {
                    text += "上楼: \(floorsAscended)\n"
                }
                if let floorsDescended = pedometerData?.floorsDescended {
                    text += "下楼: \(floorsDescended)\n"
                }
                if let currentPace = pedometerData?.currentPace {
                    text += "速度: \(currentPace)m/s\n"
                }
                if let currentCadence = pedometerData?.currentCadence {
                    text += "速度: \(currentCadence)步/秒\n"
                }
                
                //在线程中更新文本框数据
                DispatchQueue.main.async{
                    self.textView.text = text
                }
            })
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
}
